// pages/person/vipplas/index.js
const app = getApp()
import Toast from "../../../vant/weapp/toast/toast";
import clickreply from "../../../utils/clickreply.js";
Page({
  data: {
    room: [],
    index: 0,
    cardList: [],
    listIndex: -1,
    is_user_show: true,
    is_user: 0,
    showUser: false,
    chargeprice:0,
    isCardList:true
  },
  onLoad(options) {
    const that = this
    app.http.post(app.api.roomcz, {}, (res) => {
      that.setData({
        room: res.data,
        store_id: options.room_id
      })
      
      that.getCardList(options.room_id)
      if(options.room_id){
        for (let i in res.data) {        
          if (res.data[i].room_id == options.room_id) {
            that.setData({
              index: i,
            })
            return
          }
        }
      }
      
    })
  },
  //   登录
  login() {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    } else if (wx.getStorageSync('mobile') == "") {
      this.setData({
        is_user_show: false,
        is_user: 2
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    console.log(99, e)
    this.setData({
      showUser: true,
    });
    wx.setStorageSync("mobile", e.detail.mobile)
    wx.setStorageSync('userInfo', e.detail)
  },
  // 充值列表
  getCardList(store) {
    console.log(store)
    const that = this
    let store_id 
    if(store){
    
      store_id =store
     
    }else{
      store_id = that.data.room[that.data.index].room_id.toString()
    }
    
    app.http.post(app.api.addMoney, {
      store_id
    }, (res) => {
    if(res.data==""){
      that.setData({
        cardList: res.data,
        isCardList:true
      })
    }else{
      that.setData({
        cardList: res.data,
        isCardList:false
      })
    }

    })

  },
  // 切换门店
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    })
    this.getCardList()
  },

  bindHideKeyboard: function (e) {

    this.setData({
      listIndex:-1,
      chargeprice: e.detail.value
    })
  },
  choosePrice(e) {
    if(e.currentTarget.dataset.id==this.data.listIndex){
      this.setData({
        listIndex: -1,
      });
    }else{
      this.setData({
        chargeprice:0,
        listIndex: e.currentTarget.dataset.id,
      });
    }
 
  },
  // 立即充值
  rechargeOrder() {
    if (clickreply.preventDuplicateClicks(5)) return;
    const that = this
     if (wx.getStorageSync('mobile') == "") {
      that.login()
      return
    }
    let store_id = that.data.store_id;
    let chargeprice = that.data.chargeprice;
    let id =chargeprice?'0': that.data.cardList[that.data.listIndex].id.toString();
    app.http.post(app.api.rechargeOrder, {
      store_id,
      chargeprice,
      id
    }, (res) => {
      if(res.code!==200){ 
        wx.showLoading({
          title:res.message
      })
      setTimeout(function () {
          wx.hideLoading()
      },2000)
return;
      }
      if(res.data.if_fz){ 
           app.http.wecatfz(res.data.order_on, 1, (res) => {
      })
    }else{ 
         app.http.wecat(res.data.order_on, 1, (res) => {
      })}
  
    })
  }
});